home *** CD-ROM | disk | FTP | other *** search
/ AMIGA-CD 2 / Amiga-CD - Volume 2.iso / ungepackte_daten / 1993 / 2 / 02 / suchalgorithmen / listing2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-01  |  1.4 KB  |  59 lines

  1. #include <stdio.h>
  2. struct Liste {
  3.   long Nr;
  4.   char Eintrag[40];
  5.   struct Liste *Next;
  6. };
  7. struct Liste *Kopf,*Ende;
  8.  
  9. long atol(char *);
  10. char Eingabe[40];
  11.  
  12. void Init(void)
  13. {
  14.   Kopf = (struct Liste *)malloc(sizeof(struct Liste));
  15.   Ende = (struct Liste *)malloc(sizeof(struct Liste));
  16.   Kopf->Nr=0; Kopf->Eintrag[0]=0x0;
  17.   Kopf->Next=Ende; Ende->Next=Ende; Ende->Nr=-1;
  18. }
  19. void Einfuegen(long nr,char *inhalt)
  20. {
  21.   struct Liste *akt=Kopf,*new;
  22.   new = (struct Liste *)malloc(sizeof(struct Liste));
  23.   if( new ) {
  24.     strcpy(Ende->Eintrag,inhalt);
  25.     while( strcmp(inhalt,akt->Next->Eintrag) > 0 ) akt=akt->Next;
  26.     new->Next=akt->Next; akt->Next=new;
  27.     new->Nr=nr;
  28.     strcpy(new->Eintrag,inhalt);
  29.   }
  30. }
  31. long Suchen(char *eintrag)
  32. {
  33.   struct Liste *search=Kopf;
  34.   strcpy(Ende->Eintrag,eintrag);
  35.   while( strcmp(eintrag, search->Eintrag) > 0 ) search = search->Next;
  36.   if( strcmp(eintrag,search->Eintrag) ) return Ende->Nr;
  37.   return search->Nr;
  38. }
  39. main() {
  40.   long anz=0,ret;
  41.   Init();
  42.   if( Kopf && Ende ) {
  43.     while( anz < 10 ) {
  44.       printf("Eingabe des %d.ten Eintrags: ",anz+1);
  45.       gets( Eingabe );
  46.       Einfuegen(anz++,Eingabe);
  47.     }
  48.     for( anz=0; anz<3; anz++ ) {
  49.       printf("Suchen nach welchem Datensatz (Zeichenkette) ? ");
  50.       gets( Eingabe );
  51.       ret=Suchen(Eingabe);
  52.       if( ret != -1 )
  53.         printf("Gefunden. Nr. des Datensatzes: %d\n",ret+1);
  54.       else
  55.         printf("Nicht gefunden\n");
  56.     }
  57.   }
  58. }
  59.